/*
 * Lazy Load - jQuery plugin for lazy loading images
 *
 * Copyright (c) 2007-2012 Mika Tuupola
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Project home:
 *   http://www.appelsiini.net/projects/lazyload
 *
 * Version:  1.7.2
 *
 */
(function(a,b){$window=a(b),a.fn.lazyload=function(c){function f(){var b=0;d.each(function(){var c=a(this);if(e.skip_invisible&&!c.is(":visible"))return;if(!a.abovethetop(this,e)&&!a.leftofbegin(this,e))if(!a.belowthefold(this,e)&&!a.rightoffold(this,e))c.trigger("appear");else if(++b>e.failure_limit)return!1})}var d=this,e={threshold:0,failure_limit:0,event:"scroll",effect:"show",container:b,data_attribute:"original",skip_invisible:!0,appear:null,load:null};return c&&(undefined!==c.failurelimit&&(c.failure_limit=c.failurelimit,delete c.failurelimit),undefined!==c.effectspeed&&(c.effect_speed=c.effectspeed,delete c.effectspeed),a.extend(e,c)),$container=e.container===undefined||e.container===b?$window:a(e.container),0===e.event.indexOf("scroll")&&$container.bind(e.event,function(a){return f()}),this.each(function(){var b=this,c=a(b);b.loaded=!1,c.one("appear",function(){if(!this.loaded){if(e.appear){var f=d.length;e.appear.call(b,f,e)}a("<img />").bind("load",function(){c.hide().attr("src",c.data(e.data_attribute))[e.effect](e.effect_speed),b.loaded=!0;var f=a.grep(d,function(a){return!a.loaded});d=a(f);if(e.load){var g=d.length;e.load.call(b,g,e)}}).attr("src",c.data(e.data_attribute))}}),0!==e.event.indexOf("scroll")&&c.bind(e.event,function(a){b.loaded||c.trigger("appear")})}),$window.bind("resize",function(a){f()}),f(),this},a.belowthefold=function(c,d){var e;return d.container===undefined||d.container===b?e=$window.height()+$window.scrollTop():e=$container.offset().top+$container.height(),e<=a(c).offset().top-d.threshold},a.rightoffold=function(c,d){var e;return d.container===undefined||d.container===b?e=$window.width()+$window.scrollLeft():e=$container.offset().left+$container.width(),e<=a(c).offset().left-d.threshold},a.abovethetop=function(c,d){var e;return d.container===undefined||d.container===b?e=$window.scrollTop():e=$container.offset().top,e>=a(c).offset().top+d.threshold+a(c).height()},a.leftofbegin=function(c,d){var e;return d.container===undefined||d.container===b?e=$window.scrollLeft():e=$container.offset().left,e>=a(c).offset().left+d.threshold+a(c).width()},a.inviewport=function(b,c){return!a.rightofscreen(b,c)&&!a.leftofscreen(b,c)&&!a.belowthefold(b,c)&&!a.abovethetop(b,c)},a.extend(a.expr[":"],{"below-the-fold":function(c){return a.belowthefold(c,{threshold:0,container:b})},"above-the-top":function(c){return!a.belowthefold(c,{threshold:0,container:b})},"right-of-screen":function(c){return a.rightoffold(c,{threshold:0,container:b})},"left-of-screen":function(c){return!a.rightoffold(c,{threshold:0,container:b})},"in-viewport":function(c){return!a.inviewport(c,{threshold:0,container:b})},"above-the-fold":function(c){return!a.belowthefold(c,{threshold:0,container:b})},"right-of-fold":function(c){return a.rightoffold(c,{threshold:0,container:b})},"left-of-fold":function(c){return!a.rightoffold(c,{threshold:0,container:b})}})})(jQuery,window)

$(document).ready(function() {

    $('img[data-original]').lazyload({
        effect: "fadeIn"
    });
    
    var $q = $("input[name=q]");

    $("form[method=get]").submit(function(){
        var val = $q.val();
        if(val == null||val == ""){
            return false;
        }
    });
	
    $('tr td[size]').each(function(){
       var $this = $(this);
	   var p = ["B","KB","MB","GB","TB"]
	   var i = 0;
	   var c = parseInt($this.attr("size"));
	   while(c > 1024){
	     c = c/1024;
		 i++;
	   }
	   $this.html(c.toFixed(2) + "<em style='padding:1px'>" + p[i] + "</em>");
    });
    
    $('#qvod button[type=button],#bdhd button[type=button],#ed2k button[type=button]').click(function() {
        var $this = $(this);
        var protocol = $this.parents("table[id]").first().attr("id");
        var FileSize = $this.parent().first().prev().attr("size");
        var FileName = $this.parent().first().prev().prev().text();
		var v = protocol + "://" + (protocol != "ed2k" ?  (FileSize + "|" + $this.val() + "|" + FileName) : $this.val());
        if(v.StartWith("ed2k://")){
            if (window.clipboardData) {
                 window.clipboardData.clearData();
                 if(clipboardData.setData("Text", v)){
                    alert("ED2K地址复制成功！");
                 }
                 else{
                    alert("请刷新页面，重新点击，并选择允许！");
                 }
            }else{
                alert("无法复制ED2K地址！");
                //window.location.assign(v);
            }
            return false;
        }else{
			var classid = null, type = null,message = null;
			if(v.StartWith("qvod://")){
			  classid = "clsid:F3D0D36F-23F8-4682-A195-74C92B03D4AF";
			  type = "application/qvod-plugin";
			  message = "<center>请安装快播</center>";
			  v += "|";
			}
			if(v.StartWith("bdhd://")){
			  classid = "clsid:02E2D748-67F8-48B4-8AB4-0A085374BB99";
			  type = "application/bdhd-plugin";
			  message = "<center>请安装百度影音</center>";
			}
			var width =  0.63 * window.screen.availWidth, height = 0.72 * window.screen.availHeight;
			var x = (window.screen.width - width) / 2, y = (window.screen.height - height) / 2.5;
			var p = "height=" + height + ",width=" + width + ",top=" + y +",left=" + x + ",toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no";
			var win = window.open("", "player", p);
			var html = '<!DOCTYPE html><html><head><meta charset="utf-8" /><title>' + FileName + '</title></head><style>p{font-size:12px}';
			html += 'center{margin:120px 0;font-size:36px}a{color:#FFCC00}body{padding:0;margin:0;background:#333;color:#fff}</style><body>';
			html += '<object classid="'+ classid +'" width="' + width + '" height="' + height + '" id="video" name="video" onerror="javascript:this.innerHTML =\'' + message +'\'" align="center">';
			html += '<param name="url" value="' + v + '"><param name="autoplay" value="1">';
			html += '<embed url="' + v + '" type="' + type + '" width="' + width + '" height="' + height + '"></embed></object></body></html>';
			win.document.write(html);
			win.document.close();
			win.focus();
		}
        return false;
    }); 
    
    $("caption").dblclick(function(){
        var $this = $(this);
        var href = null;
        if($this.children().is("a")){
          href = $this.children("a").first().attr("href");
        }
        else{
          href = window.location.href;
        }
        $this.append($("<a style='float:right'>edit</a>").attr("href",href.replace("/category/","/edit/")));
    });
    
    $(".search li").dblclick(function(){
        var $this = $(this).css("background","#F3FAFF");
        var href = null;
        if($this.children().is("a")){
          href = $this.children("a").first().attr("href");
        }
        else{
          href = window.location.href;
        }
        $this.append($("<a style='float:right'>delete</a>").attr("href",href.replace("/category/","/delete/")).click(function(){
            if(confirm('您确认要删除这部电视剧（电影）?')){
                $.get($(this).attr("href"), function(data){
                  alert(data);
                  return false;
                });
            }
            return false;
        }));
    });
});

String.prototype.StartWith = function(s){
	if(s == null||s == ""|| this.length == 0 || s.length > this.length)
		return false;
	if(this.substr(0,s.length)==s)
		return true;
	else
		return false;
	return true;
};

